package com.jspgou.cms.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.jspgou.common.hibernate3.Finder;
import com.jspgou.common.hibernate3.HibernateBaseDao;
import com.jspgou.common.page.Pagination;
import com.jspgou.cms.dao.AdspaceDao;
import com.jspgou.cms.entity.Adspace;

@Repository
public class AdspaceDaoImpl extends HibernateBaseDao<Adspace, Long>
		implements AdspaceDao {
	
	public Adspace findById(Long id) {
		Adspace entity = get(id);
		return entity;
	}
	
	public Pagination getPagination(Long storeId,int pageNo, int pageSize){
		Finder f=Finder.create("from Adspace bean where 1=1");
		f.append(" and bean.store.id=:storeId").setParam("storeId", storeId);
		return find(f,pageNo,pageSize);
	}
	
	@SuppressWarnings("unchecked")
	public List<Adspace> getList(){
		Finder f=Finder.create("from Adspace bean");
	      return find(f);
	}
	
	@SuppressWarnings("unchecked")
	public List<Adspace> getListByStoreId(Long storeId,Boolean enabled){
		  Finder f=Finder.create("from Adspace bean where 1=1");
		  if(storeId!=null){
			  f.append(" and bean.store.id=:storeId").setParam("storeId", storeId);
		  }else{
			  f.append(" and bean.store.id is null");
		  }
		  if(enabled!=null){
			  f.append(" and bean.enabled=:enabled").setParam("enabled", enabled);
		  }
	      return find(f);
	}

	public Adspace save(Adspace bean) {
		getSession().save(bean);
		return bean;
	}

	public Adspace deleteById(Long id) {
		Adspace entity = super.get(id);
		if (entity != null) {
			getSession().delete(entity);
		}
		return entity;
	}

	@Override
	protected Class<Adspace> getEntityClass() {
		return Adspace.class;
	}

}